using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using FarseerPhysics;
using FarseerPhysics.Common;
using FarseerPhysics.Collision;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Factories;
using FarseerPhysics.Collision.Shapes;


namespace MySimpleFarseerMonogame
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class MyBall : Microsoft.Xna.Framework.GameComponent
    {

        private Texture2D texture;
        private ContentManager contentManager;
        private SpriteBatch batch;
        public Body body;
        private Int32 circleRadius = 25;
        private World world;


        public MyBall(Game game,ContentManager content,SpriteBatch batch,World world)
            : base(game)
        {
            // TODO: Construct any child components here
            this.contentManager = content;
            this.batch = batch;
            body = BodyFactory.CreateBody(world);
            body.Position = new Vector2(50, 50);
            body.BodyType = BodyType.Dynamic;
            CircleShape ballShape = new CircleShape(circleRadius,1);
            Fixture ballFixture =body.CreateFixture(ballShape);
            this.world = world;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
            texture = contentManager.Load<Texture2D>("AquaBall");
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
           
            base.Update(gameTime);
        }

        public void Draw() {
            batch.Draw(texture, GetRectangleByPos(body.Position), Color.White);
        }

        private Rectangle GetRectangleByPos(Vector2 SimPos) {
            Int32 posX; Int32 posY;
            posX = Helper.ConvertSimToDisplay(SimPos.X);
            posY = Helper.ConvertSimToDisplay(SimPos.Y);
            return new Rectangle(Convert.ToInt32(posX - circleRadius), Convert.ToInt32(posY - circleRadius), Convert.ToInt32(circleRadius * 2), Convert.ToInt32(circleRadius * 2));
        }

    }
}
